<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams">
      <el-form-item label="样本类型编号" prop="sampTypeCode">
        <el-input
          v-model="queryParams.sampTypeCode"
          clearable
          placeholder="请输入样本类型编号"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="样本类型名称" prop="sampTypeName">
        <el-input
          v-model="queryParams.sampTypeName"
          clearable
          placeholder="请输入样本类型名称"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="描述" prop="sampTypeDesc">
        <el-input
          v-model="queryParams.sampTypeDesc"
          clearable
          placeholder="请输入描述"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['biobank:sampleType:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="sampleTypeList"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      default-expand-all
      row-key="sampTypeId"
    >
      <el-table-column label="样本类型编号" prop="sampTypeCode"/>
      <el-table-column align="center" label="样本类型名称" prop="sampTypeName"/>
      <el-table-column :formatter="typeSpecFormat" align="center" label="样本形态" prop="sampTypeSpec"/>
      <el-table-column align="center" label="同层级排序" prop="sampTypeSort"/>
      <el-table-column align="center" label="默认单位" prop="sampTypeDefaultUnit"/>
      <el-table-column align="center" label="默认量" prop="sampTypeDefaultQuantity"/>
      <el-table-column align="center" label="默认复份数量" prop="sampTypeDefaultDuplqty"/>
      <el-table-column align="center" label="图标" prop="icon" width="100">
        <template slot-scope="scope">
          <svg-icon :icon-class="scope.row.sampTypeImgUrl" style="height: 24px;width: 24px;"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="保质天数" prop="sampTypeShelfLife"/>
      <el-table-column align="center" label="低使用率年限" prop="sampTypeLowUseYear"/>
      <el-table-column :show-overflow-tooltip="true" align="center" label="描述" prop="sampTypeDesc"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['biobank:sampleType:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['biobank:sampleType:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改样本类型 对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="父样本类型" prop="parentId">
          <treeselect v-model="form.parentId" :normalizer="normalizer" :options="sampleTypeOptions"
                      placeholder="请选择父样本类型"
          />
        </el-form-item>
        <el-form-item label="样本类型编号" prop="sampTypeCode">
          <el-input v-model="form.sampTypeCode" placeholder="请输入样本类型编号"/>
        </el-form-item>
        <el-form-item label="样本类型名称" prop="sampTypeName">
          <el-input v-model="form.sampTypeName" placeholder="请输入样本类型名称"/>
        </el-form-item>
        <el-form-item label="样本形态" prop="sampTypeSpec">
          <el-select v-model="form.sampTypeSpec" clearable placeholder="样本形态" size="small" style="width: 100%">
            <el-option
              v-for="dict in typeSpecOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="同层级排序" prop="sampTypeSort">
          <el-input-number v-model="form.sampTypeSort" :min="1" controls-position="right" placeholder="请输入样本类型同层级排序"
                           style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="默认单位" prop="sampTypeDefaultUnit">
          <el-input v-model="form.sampTypeDefaultUnit" placeholder="请输入默认单位"/>
        </el-form-item>
        <el-form-item label="默认量" prop="sampTypeDefaultQuantity">
          <el-input v-model="form.sampTypeDefaultQuantity" oninput="value=value.replace(/[^0-9.]/g,'')"
                    placeholder="请输入默认量"
          />
        </el-form-item>
        <el-form-item label="默认复份数量" prop="sampTypeDefaultDuplqty">
          <el-input-number v-model="form.sampTypeDefaultDuplqty" :min="1" controls-position="right"
                           placeholder="请输入默认复份数量" style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="图标" prop="sampTypeImgUrl">
          <el-input v-model="form.sampTypeImgUrl" placeholder="点击选择图标" readonly>
            <svg-icon
              v-if="form.sampTypeImgUrl"
              slot="prefix"
              :icon-class="form.sampTypeImgUrl"
              class="el-input__icon"
              style="height: 38px;width: 22px;"
            />
            <el-button slot="append" icon="el-icon-thumb" @click="openIconsDialog(form.sampTypeImgUrl)">
              选择
            </el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="保质天数" prop="sampTypeShelfLife">
          <el-input v-model="form.sampTypeShelfLife" oninput="value=value.replace(/[^0-9]/g,'')" placeholder="请输入保质天数"/>
        </el-form-item>
        <el-form-item label="低使用率年限" prop="sampTypeLowUseYear">
          <el-input v-model="form.sampTypeLowUseYear" oninput="value=value.replace(/[^0-9.]/g,'')"
                    placeholder="请输入低使用率年限"
          />
        </el-form-item>
        <el-form-item label="描述" prop="sampTypeDesc">
          <el-input v-model="form.sampTypeDesc" placeholder="请输入描述"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <samp-icon-select :current="activeIcon" :visible.sync="iconsVisible" @select="setIcon"/>
  </div>
</template>

<script>
import { addSampleType, delSampleType, getSampleType, listSampleType, updateSampleType } from '@/api/biobank/sampleType'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import SampIconSelect from '@/components/SampIconSelect'

export default {
  name: 'SampleType',
  components: {
    Treeselect, SampIconSelect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 样本类型 表格数据
      sampleTypeList: [],
      // 样本类型 树选项
      sampleTypeOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 选择样本图标弹出层
      iconsVisible: false,
      // 当前的样本
      activeIcon: undefined,
      // 查询参数
      queryParams: {
        parentId: null,
        sampTypeCode: null,
        sampTypeName: null,
        sampTypeSpec: null,
        sampTypeSort: null,
        sampTypeDefaultUnit: null,
        sampTypeDefaultQuantity: null,
        sampTypeDefaultDuplqty: null,
        sampTypeImgUrl: null,
        sampTypeDesc: null,
        sampTypeShelfLife: null,
        sampTypeLowUseYear: null
      },
      // 样本形态
      typeSpecOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentId: { required: true, trigger: 'change', message: '父样本类型不能为空' },
        sampTypeCode: [{ required: true, trigger: 'blur', message: '样本类型编号不能为空' }, {
          max: 50,
          trigger: 'blur',
          message: '样本类型编号不能超过50个字符'
        }],
        sampTypeName: [{ required: true, trigger: 'blur', message: '样本类型名称不能为空' }, {
          max: 50,
          trigger: 'blur',
          message: '样本类型名称不能超过50个字符'
        }],
        sampTypeSpec: { required: true, trigger: 'change', message: '样本形态不能为空' },
        sampTypeDefaultUnit: [{ required: true, trigger: 'blur', message: '默认单位不能为空' }, {
          max: 10,
          trigger: 'blur',
          message: '默认单位不能超过10个字符'
        }],
        sampTypeDefaultQuantity: { required: true, trigger: 'blur', message: '默认量不能为空' },
        sampTypeDefaultDuplqty: { required: true, trigger: 'blur', message: '默认复份数量不能为空' },
        sampTypeImgUrl: { required: true, trigger: 'change', message: '图标不能为空' },
        sampTypeDesc: { max: 100, trigger: 'blur', message: '样本类型秒速不能超过100个字符' }
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('sample_spec').then(response => {
      this.typeSpecOptions = response.data
    })
  },
  methods: {
    // 选择样本图标
    setIcon(val) {
      this.form.sampTypeImgUrl = val
    },
    openIconsDialog(icon) {
      this.iconsVisible = true
      this.activeIcon = icon
    },
    // 样本形态字典翻译
    typeSpecFormat(row, column) {
      return this.selectDictLabel(this.typeSpecOptions, row.sampTypeSpec)
    },
    /** 查询样本类型 列表 */
    getList() {
      this.loading = true
      listSampleType(this.queryParams).then(response => {
        this.sampleTypeList = this.handleTree(response.data, 'sampTypeId', 'parentId')
        this.loading = false
      })
    },
    /** 转换样本类型 数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.sampTypeId,
        label: node.sampTypeName,
        children: node.children
      }
    },
    /** 查询样本类型 下拉树结构 */
    getTreeselect() {
      listSampleType().then(response => {
        this.sampleTypeOptions = []
        const data = { sampTypeId: 0, sampTypeName: '顶级节点', children: [] }
        data.children = this.handleTree(response.data, 'sampTypeId', 'parentId')
        this.sampleTypeOptions.push(data)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        sampTypeId: null,
        parentId: null,
        sampTypeCode: null,
        sampTypeName: null,
        sampTypeSpec: null,
        sampTypeSort: null,
        sampTypeDefaultUnit: null,
        sampTypeDefaultQuantity: null,
        sampTypeDefaultDuplqty: null,
        sampTypeImgUrl: null,
        sampTypeDesc: null,
        sampTypeShelfLife: null,
        sampTypeLowUseYear: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.getTreeselect()
      this.open = true
      this.title = '添加样本类型 '
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.getTreeselect()
      if (row != null) {
        this.form.parentId = row.sampTypeId
      }
      getSampleType(row.sampTypeId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改样本类型 '
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.sampTypeId != null) {
            updateSampleType(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addSampleType(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除样本类型 编号为"' + row.sampTypeId + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delSampleType(row.sampTypeId)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    }
  }
}
</script>
